package services

import (
	"awesome-devops/applications/backend/modules/services"
	"awesome-devops/applications/rpc/define"
	"awesome-devops/applications/tools"
	"context"
	"gitee.com/jjawesomejj/awesome-util/helper/commonhelper"
	"gitee.com/jjawesomejj/awesome-util/helper/timeHelper"
)

type Runner struct {
	define.UnimplementedRunnerServiceServer
}

func (runner *Runner) RunDeployTask(context context.Context, request *define.RunnerRequest) (*define.RunnerResponse, error) {
	defer func() {
		if err := recover(); err != nil {
			tools.GetLogger().Error("rpcError:" + commonhelper.PrintStackTrace(err))
			return
		}
	}()
	id := request.Id
	service := services.DeployService{}
	res := service.Publish(int(id), timeHelper.NowFloat())
	response := define.RunnerResponse{}
	switch res.(type) {
	case error:
		response.Code = 1000
		response.Message = res.(error).Error()
	default:
		response.Code = 0
		response.Message = "success"
	}
	return &response, nil
}
